<?php
if (!defined('ROOT')) exit('SYTbHelper');
//签到相关
class sign_model extends base {
	private $tblist;
	static private $_instance=NULL;
	static public function getInstance () {
		if (!self::$_instance) self::$_instance=new self;
		return self::$_instance;
	}
	//初始化函数
	public function __construct () {
		parent::init();
		$_ENV['_now']='sign';
		$this->tblist=$this->db('get','tblist');
	}
	//清空贴吧列表
	private function tb_flush () {
		$this->tblist=array();
		$this->db('set','tblist',array());
	}
	//设置贴吧列表
	private function tb_set ($tb) {
		$this->tblist=$tb;
		$this->db('set','tblist',$tb);
	}
	//获取贴吧数目
	private function tb_getnum () {
		return count($this->tblist);
	}
	//获取一个贴吧
	private function tb_getone () {
		return each($this->tblist);
	}
	//移除一个贴吧
	private function tb_removeone ($id) {
		global $kv;
		unset($this->tblist[$id]);
		$this->db('set','tblist',$this->tblist);
	}
	//浏览器签到，未启用
	function dubrowser_sign ($tieba,$fid,$id){
		return FALSE;
		global $out;
		$url=$this->mkurl('mo/q---0126785D9262B60C89D8A1C39D172E1B:FG=1--1-3-0--2--wapp_1402018681523_885/m?lp=9001&kw='.$tieba);
		$get_url=curl_get($url);
		echo $get_url;
		preg_match_all('/<a href="(.*?)">签到<\/a>/',$get_url,$matches);
		if (isset($matches[1][0])) {
			$s=htmlspecialchars_decode($matches[1][0]);
			$sign_url=$this->mkurl(trim($s,'/'));
			$get_sign=curl_get($sign_url);
			if (strpos($get_sign,'签到成功')!==FALSE) {
				$out.='【'.iconv('GB2312','UTF-8',urldecode($tieba)).'】吧：签到成功<br/>';
				removeonetb($id);
			} elseif (strpos($get_url,'<span >已签到</span>')!==FALSE){
				removeonetb($id);
				$out.='【'.iconv('GB2312','UTF-8',urldecode($tieba)).'】吧：之前已经签到<br/>';
			} else{
				$out.='【'.iconv('GB2312','UTF-8',urldecode($tieba)).'】吧：未知错误<br/>';
			}
		}
	}
	//客户端签到
	private function client_sign ($tieba,$fid,$id) {
		$tieba=urldecode($tieba);
		$post=array(
			'fid'=>$fid,
			'kw'=>$tieba,
		);
		$post=client_mkInfo($post);
		//请求
		$sign_json=curl_get($this->mkurl('c/c/forum/sign','c'),array('post'=>$post));
		$res=jd($sign_json);
		if (!$res) {
			echo '<li class="list-group-item"><strong>'.iconv('GB2312', 'UTF-8',$tieba).'吧</strong>：JSON解析错误</li>';
			return FALSE;
		}
		if ($res['user_info']) {
			$exp=$res['user_info']['sign_bonus_point'];
			echo '<li class="list-group-item list-group-item-success"><strong>',iconv('GB2312', 'UTF-8',$tieba),'吧</strong>：签到成功，经验值上升',$exp,'</li>';
			$this->tb_removeone($id);
		} else {
			switch($res['error_code']){
				case '160002': //已经签过
					echo '<li class="list-group-item list-group-item-info"><strong>',iconv('GB2312', 'UTF-8',$tieba),'吧</strong>：之前已经签到</li>';
					$this->tb_removeone($id);
					break;
				case '1': //未登录
					echo '<li class="list-group-item list-group-item-danger"><strong>Cookie不正确</strong></li>';
					break;
				case '340006': //贴吧出现问题
					echo '<li class="list-group-item list-group-item-warning"><strong>',iconv('GB2312', 'UTF-8',$tieba),'吧</strong>：贴吧出现错误，可能该吧被封禁</li>';
					$this->tb_removeone($id);
					break;
				default:
					echo '<li class="list-group-item"><strong>',iconv('GB2312', 'UTF-8',$tieba),'吧</strong>：未知错误 （错误代码：',$res['error_code'],'。错误信息：',$res['error_msg'],'）</li>';
					break;
			}
		}
	}
	//签到
	private function sign () {
		if (!$this->is_login()) $this->DisplayError('Cookie错误');
		sae_debug('Sign start');
		$i=0;
		ob_start();
		while ($row=$this->tb_getone()) {
			if ($i==10) break;
			$i++;
			list($id,$vals)=$row;
			if (SIGN_METHOD==1) $this->client_sign($vals['name'],$vals['fid'],$id);
			// elseif ($method==2) client_sign($vals['name'],$vals['fid'],$id);
			sleep(0.3);
			//休息0.3秒后重复一次
		}
		$num=$this->tb_getnum();
		if ($num!=0) $continue='<div class="alert alert-success"><a href="?m=sign&a=sign" class="btn btn-success">还剩下'.$num.'个贴吧没有签到，点击这里继续</a></div>';
		else $continue='<div class="alert alert-success">所有贴吧已签到</div>';
		sae_debug('Sign finished');
		if (defined('CRON')) {
			ob_end_clean();
			echo 'CRON';
			if ($num!=0) {
				sae_debug('Go to add task');
				task_add('sign');
			}
		} else {
			$info=ob_get_clean();
			include(TPL.'sign_result.html');
		}
	}
	//获取“我喜欢的吧”
	private function getmylike (){
		$this->tb_flush(); //清空贴吧列表
		$is_login=$this->is_login();
		if (!$is_login) $this->DisplayError('Cookie错误');
		//获取“我喜欢的吧”
		$pn=0;
		$tblist=array();
		while (true){
			$pn++;
			$mylikeurl=$this->mkurl('f/like/mylike?pn='.$pn);
			$result=curl_get($mylikeurl);
			$pre_reg='/balvid="(.*?)" balvname="(.*?)" tbs="(.*?)">/';
			preg_match_all($pre_reg,$result,$matches);
			$onepagenum=count($matches[0]);
			for ($i=1;$i<=$onepagenum;$i++) {
				$tblist[]=array(
					'name'=>$matches[2][$i-1],
					'fid'=>$matches[1][$i-1],
				);
			}
			if ($onepagenum==0) break;
		}
		$this->tb_set($tblist);
		$i=count($tblist);
		$tmps=strval($i/15);
		$times=intval($tmps);
		$day=date('Ymd');
		$this->setval('lastsign',$day);
		if (defined('CRON')) echo 'CRON';
		else $this->ShowTips("一共[ $i ]个贴吧，预计需要 $times 次签到",'success');
	}
	//实际动作
	public function onsign () {
		$this->sign();
	}
	//实际动作
	public function onilike () {
		$this->getmylike();
	}
	//CRON
	public function cron () {
		$first=$this->getval('lastsign');
		if ($first!=date('Ymd')) $this->getmylike();
		$this->sign();
	}
}
?>